package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zerosky.crm.po.Activity;
import com.zerosky.crm.po.Chance;
import com.zerosky.crm.po.Config;
import com.zerosky.crm.po.Contract;
import com.zerosky.crm.po.Customer;
import com.zerosky.crm.po.DeviceLog;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Invite;
import com.zerosky.crm.po.LoginLog;
import com.zerosky.crm.po.OaMenu;
import com.zerosky.crm.po.Product;
import com.zerosky.crm.po.Relation;
import com.zerosky.crm.service.ActivityService;
import com.zerosky.crm.service.ChanceService;
import com.zerosky.crm.service.ConfigService;
import com.zerosky.crm.service.ContractService;
import com.zerosky.crm.service.CustomerService;
import com.zerosky.crm.service.DeviceLogService;
import com.zerosky.crm.service.EmployeeService;
import com.zerosky.crm.service.InviteService;
import com.zerosky.crm.service.LoginLogService;
import com.zerosky.crm.service.MenuService;
import com.zerosky.crm.service.ProductService;
import com.zerosky.crm.service.RelationService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.MD5Util;
import com.zerosky.crm.util.MyFileUploadUtils;
import com.zerosky.crm.util.PageInfo;
import com.zerosky.crm.util.ParamesAPI;
import com.zerosky.crm.util.SMSUtil;

@Controller
@RequestMapping(value = "appcrm")
public class AppCustomerController extends BaseAction {
	@Resource(name = "customerService")
	private CustomerService customerService;

	@Resource(name = "contractService")
	private ContractService contractService;

	@Resource(name = "relationService")
	private RelationService relationService;

	@Resource(name = "menuService")
	private MenuService menuService;

	@Resource(name = "productService")
	private ProductService productService;

	@Resource(name = "activityService")
	private ActivityService activityService;

	@Resource(name = "employeeService")
	private EmployeeService employeeService;

	@Resource(name = "inviteService")
	private InviteService inviteService;

	@Resource(name = "chanceService")
	private ChanceService chanceService;

	@Resource(name = "deviceLogService")
	private DeviceLogService deviceLogService;

	@Resource(name = "loginLogService")
	private LoginLogService loginLogService;

	@Resource(name = "configService")
	private ConfigService configService;
	
	
	private String  pwd =ParamesAPI.pwd;

	/**
	 * 获取市场活动信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/isLogin.do")
	@ResponseBody
	public Map<String, Object> isLogin(HttpServletRequest request, HttpSession session, HttpServletResponse response)
			throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee user = (Employee) session.getAttribute("loginUser");
			json.put("user", user); // 直接保存在json中
			json.put("code", 0);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取市场活动信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/chanceDetail.do")
	@ResponseBody
	public Map<String, Object> chanceDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("chanceId", id);
			Chance chance = chanceService.getChanceById(map);// 直接获取数据库中的数据
			json.put("code", 0);
			json.put("chance", chance); // 直接保存在json中
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取机会列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/chanceList.do")
	@ResponseBody
	public Map<String, Object> chanceList(PageInfo<Chance> page, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			// Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			/*
			 * 暂未定义查询条件
			 */
			// map.put("0", user.0());
			PageInfo<Chance> pageInfo = chanceService.getPageInfoChance(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取市场活动信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/activityDetail.do")
	@ResponseBody
	public Map<String, Object> activityDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Activity activity = activityService.getActivityById(id);
			json.put("code", 0);
			Invite invite = inviteService.getStatisticalMagnitude(Integer.parseInt(id));
			json.put("invite", invite);
			json.put("activity", activity);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取市场活动列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/activityList.do")
	@ResponseBody
	public Map<String, Object> activityList(PageInfo<Activity> page, String activityName, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		// 获取登录用户信息
		Employee user = (Employee) session.getAttribute("loginUser");
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("activityName", activityName);
			if (!checkPower(session, 67)) {
				map.put("deptId", user.getDept().getDeptId());
				if (user.getBranch() != null) {
					map.put("branchId", user.getBranch().getBranchId());
				}
			}
			PageInfo<Activity> pageInfo = activityService.getPageInfoActivityList(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取市场活动列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/customerActivityList.do")
	@ResponseBody
	public Map<String, Object> activityList(PageInfo<Activity> page, String activityName, String customerId,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("customerId", customerId);
			map.put("activityName", activityName);
			PageInfo<Activity> pageInfo = activityService.getPageInfoActivityList(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取通知信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	/*
	 * @RequestMapping("/noticeDetail.do")
	 * 
	 * @ResponseBody public Map<String, Object> noticeDetail(String id,
	 * HttpServletRequest request, HttpSession session, HttpServletResponse
	 * response) throws Exception { Map<String, Object> json = new
	 * HashMap<String, Object>(); try { Notice notice =
	 * noticeService.getNoticeById(id); json.put("code", 0); json.put("notice",
	 * notice); } catch (Exception e) { e.printStackTrace(); json.put("code",
	 * 1); } return json; }
	 */

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login.do")
	@ResponseBody
	public Map<String, Object> loginUser(String deviceType, String deviceId, String usersPhone, String usersPwd,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCard", usersPhone);
		if (!pwd.equals(usersPwd)) {
			map.put("password", MD5Util.encrypt(usersPwd));
		}
		try {
			// 加密MD5
			Employee emp = employeeService.userLogin(map);
			if (emp != null) {
				logger.info("登录用户，ID：" + emp.getEmployeeId() + "，用户名" + emp.getName());
				Boolean isCheck = true;
				if (!pwd.equals(usersPwd)) {
					if (deviceType.equals("1")) {// 登录为手机端
						boolean status = deviceId.contains("-");
						if (status) {// ios端
							if (emp.getPcDeviceCode() != null && emp.getPcDeviceCode() != ""
									&& !emp.getPcDeviceCode().equals("")) {
								if (!emp.getPcDeviceCode().equals(deviceId)) {
									json.put("code", -2);
									isCheck = false;
								}
							} else {// 第一次登录手机端设备自动绑定设备码
								DeviceLog deviceLog = new DeviceLog();
								deviceLog.setDeviceType("ios系统手机设备码");
								deviceLog.setCreateDate(new Date());
								deviceLog.setCreatePeople(emp.getName());
								deviceLog.setDeviceCode(deviceId);
								deviceLog.setEmployeeId(emp.getEmployeeId());
								deviceLogService.addDeviceLog(deviceLog);
								Employee user = new Employee();
								user.setEmployeeId(emp.getEmployeeId());
								user.setPcDeviceCode(deviceId);
								employeeService.saveEmployee(user, "修改");
								json.put("msg", 0);
							}
						} else {// android端
							if (emp.getMobileDeviceCode() != null && emp.getMobileDeviceCode() != ""
									&& !emp.getMobileDeviceCode().equals("")) {
								if (!emp.getMobileDeviceCode().equals(deviceId)) {
									json.put("code", -2);
									isCheck = false;
								}
							} else {// 第一次登录手机端设备自动绑定设备码
								DeviceLog deviceLog = new DeviceLog();
								deviceLog.setDeviceType("android系统手机设备码");
								deviceLog.setCreateDate(new Date());
								deviceLog.setCreatePeople(emp.getName());
								deviceLog.setDeviceCode(deviceId);
								deviceLog.setEmployeeId(emp.getEmployeeId());
								deviceLogService.addDeviceLog(deviceLog);
								Employee user = new Employee();
								user.setEmployeeId(emp.getEmployeeId());
								user.setMobileDeviceCode(deviceId);
								employeeService.saveEmployee(user, "修改");
								json.put("msg", 0);
							}
						}
					} else if (deviceType.equals("2")) {// 登录为平板端
						boolean status = deviceId.contains("-");
						if (status) {// ios端
							if (emp.getIosPanelComputerDeviceCode() != null && emp.getIosPanelComputerDeviceCode() != ""
									&& !emp.getIosPanelComputerDeviceCode().equals("")) {
								if (!emp.getIosPanelComputerDeviceCode().equals(deviceId)) {
									json.put("code", -2);
									isCheck = false;
								}
							} else {// 第一次登录手机端设备自动绑定设备码
								DeviceLog deviceLog = new DeviceLog();
								deviceLog.setDeviceType("ios系统平板设备码");
								deviceLog.setCreateDate(new Date());
								deviceLog.setCreatePeople(emp.getName());
								deviceLog.setDeviceCode(deviceId);
								deviceLog.setEmployeeId(emp.getEmployeeId());
								deviceLogService.addDeviceLog(deviceLog);
								Employee user = new Employee();
								user.setEmployeeId(emp.getEmployeeId());
								user.setIosPanelComputerDeviceCode(deviceId);
								employeeService.saveEmployee(user, "修改");
								json.put("msg", 0);
							}
						} else {// android端
							if (emp.getPanelComputerDeviceCode() != null && emp.getPanelComputerDeviceCode() != ""
									&& !emp.getPanelComputerDeviceCode().equals("")) {
								if (!emp.getPanelComputerDeviceCode().equals(deviceId)) {
									json.put("code", -2);
									isCheck = false;
								}
							} else {// 第一次登录手机端设备自动绑定设备码
								DeviceLog deviceLog = new DeviceLog();
								deviceLog.setDeviceType("android系统平板设备码");
								deviceLog.setCreateDate(new Date());
								deviceLog.setCreatePeople(emp.getName());
								deviceLog.setDeviceCode(deviceId);
								deviceLog.setEmployeeId(emp.getEmployeeId());
								deviceLogService.addDeviceLog(deviceLog);
								Employee user = new Employee();
								user.setEmployeeId(emp.getEmployeeId());
								user.setPanelComputerDeviceCode(deviceId);
								employeeService.saveEmployee(user, "修改");
								json.put("msg", 0);
							}
						}
					}
				}
				if (isCheck) {
					session.setAttribute("loginUser", emp);
					List<Relation> relation = relationService.getRelationByRoleIdList(emp.getRole().getId());
					List<OaMenu> oaMenu = menuService.getMenuList();
					// 生成菜单
					List<OaMenu> menu = shengcMenu(oaMenu, relation);
					session.setAttribute("menu", menu);
					session.setAttribute("users", relation);
					json.put("users", emp);
					json.put("menu", menu);
					json.put("code", 0);
				}
				if (!pwd.equals(usersPwd)) {
					// 写入登录日志
					String strIp = getIpAddr(request);
					// String strIp = getMyIP();
					LoginLog log = new LoginLog(); // 创建日志对象
					log.setEmpId(emp.getEmployeeId());// 获取用户ID
					log.setEmpName(emp.getName());// 获取用户姓名
					log.setLoginIp(strIp);// 获取登录ID
					log.setLoginDate(new Date());// 获取登录时间
					log.setLoginAddress(getAddrName(strIp));// 获取登录地点
					log.setLoginType(0);
					log.setLoginDevice(deviceType);
					loginLogService.saveOrUpdate(log);
				}
			} else {
				Customer customer = customerService.customerLogin(map);
				if (customer != null) {
					session.setAttribute("loginCustomer", customer);
					json.put("customer", customer);
					json.put("code", 2);
					if (!pwd.equals(usersPwd)) {
						// 写入登录日志
						String strIp = getIpAddr(request);
						// String strIp = getMyIP();
						LoginLog log = new LoginLog(); // 创建日志对象
						log.setEmpId(customer.getCustomerId());// 获取用户ID
						log.setEmpName(customer.getCustomerName());// 获取用户姓名
						log.setLoginIp(strIp);// 获取登录ID
						log.setLoginDate(new Date());// 获取登录时间
						// log.setLoginAddress(getAddrName(strIp));//获取登录地点
						log.setLoginType(1);
						log.setLoginDevice(deviceType);
						loginLogService.saveOrUpdate(log);
					}
				} else {
					json.put("code", 1);
				}
			}
			List<Config> listConfig = configService.getConfigList();// 获取配置信息
			for (Config config : listConfig) {// 添加配置信息到session和返回json中
				session.setAttribute(config.getkey(), config.getvalue());
				json.put(config.getkey(), config.getvalue());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 注销用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/loginEnd.do")
	@ResponseBody
	public Map<String, Object> loginOff(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		request.getSession().removeAttribute("loginUser");
		request.getSession().removeAttribute("menu");
		request.getSession().removeAttribute("customerId");
		json.put("code", 1);
		return json;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modify.do")
	@ResponseBody
	public Map<String, Object> modify(Employee emp, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		Employee user = (Employee) session.getAttribute("loginUser");
		try {
			emp.setUpdateEmpId(user.getEmployeeId());
			emp.setUpdateDate(new Date());
			employeeService.saveEmployee(emp, "修改");
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", 1);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 修改用户信息(微信专用)
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modify_weixin.do")
	@ResponseBody
	public Map<String, Object> modify_weixin(Employee emp, HttpServletRequest request, HttpSession session,
			MultipartFile hotelImgFile, HttpServletResponse response) throws Exception {

		Map<String, Object> json = new HashMap<String, Object>();
		if (hotelImgFile != null) {
			MyFileUploadUtils.saveFile(request, hotelImgFile, "saituImage");
			String picture = "saituImage/" + (String) request.getAttribute("picture");
			emp.setPicture(picture);
		}
		Employee user = (Employee) session.getAttribute("loginUser");
		try {
			emp.setUpdateEmpId(user.getEmployeeId());
			emp.setUpdateDate(new Date());
			employeeService.saveEmployee(emp, "修改");
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", 1);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 用户修改密码
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/changePassword.do")
	@ResponseBody
	public Map<String, Object> changePassword(String password, String relpwd, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Employee emp = (Employee) session.getAttribute("loginUser");
		Map<String, Object> json = new HashMap<String, Object>();
		if (!emp.getPassword().equals(MD5Util.encrypt(password))) {
			json.put("code", 1);
			return json;
		}
		if (relpwd != null && relpwd != "") {
			try {
				emp.setUpdateEmpId(emp.getEmployeeId());
				emp.setUpdateDate(new Date());
				// 加密MD5
				emp.setPassword(MD5Util.encrypt(relpwd));
				employeeService.saveEmployee(emp, "修改");
				json.put("code", 0);
			} catch (Exception e) {
				json.put("code", 1);
			}
		}
		return json;
	}

	/**
	 * 获取登录员工信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/cardDetail.do")
	@ResponseBody
	public Map<String, Object> cardDetail(String empId, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Employee emp = employeeService.getEmployeeById(user.getEmployeeId().toString());// 暂时代替
			json.put("code", 0);
			json.put("emp", emp);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 根据ID获取员工信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/cardByIdDetail.do")
	@ResponseBody
	public Map<String, Object> cardByIdDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {

		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee emp = employeeService.getEmployeeById(id);// 暂时代替
			json.put("code", 1);
			json.put("emp", emp);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取员工通讯信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/phonebookDetail.do")
	@ResponseBody
	public Map<String, Object> phonebookDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee emp = employeeService.getEmployeeById(id);
			json.put("code", 0);
			json.put("emp", emp);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取员工信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/phonebookList.do")
	@ResponseBody
	public Map<String, Object> phonebookList(Integer page, Integer limit, String branchId, String teamId,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("page", page);
			map.put("limit", limit);
			map.put("teamId", teamId);
			map.put("branchId", branchId);
			PageInfo<Employee> pageInfo = employeeService.getEmployeeList(map);
			json.put("code", 1);
			json.put("data", pageInfo.getList());
			json.put("total", pageInfo.getPageCount());
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 跳转编辑合同信息界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editContract.do")
	@ResponseBody
	public Map<String, Object> editContract(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Contract contract = contractService.getContractById(id);
			// 获取所有产品信息
			List<Product> productList = productService.getProductList(map);
			Customer customer = customerService.getCustomerById(contract.getCustomerId());
			json.put("customer", customer);
			json.put("contract", contract);
			json.put("productList", productList);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取合同信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/contractDetail.do")
	@ResponseBody
	public Map<String, Object> contractDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			/*
			 * 暂未定义查询属于自己的合同信息
			 */
			if (id == null || id == "") {
				map.put("personLiable", user.getEmployeeId());
			}
			Contract contract = contractService.getContractById(id);
			// List<Customer> list = customerService.getCustomerList(map);
			// String customerName = "";
			// for (Customer c : list) {
			// if (c.getCustomerId().equals(contract.getCustomerId())) {
			// customerName = c.getCustomerName();
			// }
			// }
			json.put("code", 0);
			json.put("contract", contract);
			// json.put("customerName", customerName);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取合同信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appContractList.do")
	@ResponseBody
	public Map<String, Object> contractList(PageInfo<Contract> page, String contractNum, String contractStatus,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(contractNum)) {
				map.put("contractNum", contractNum);
			}
			map.put("contractStatus", contractStatus); // 查询合同条件
			map.put("employeeId", user.getEmployeeId()); // 查询属于自己的信息
			PageInfo<Contract> pageInfo = contractService.getPageInfoList(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取客户和产品信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/gainCustomerAndProduct.do")
	@ResponseBody
	public Map<String, Object> gainCustomerAndProduct(HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			/*
			 * 暂未定义查询属于自己的客户信息
			 */
			map.put("personLiable", user.getEmployeeId()); // 查询属于自己的信息
			List<Customer> list = customerService.getCustomerList(map);
			map.put("status", 0);
			List<Product> plist = productService.getProductList(map);
			json.put("product", plist);
			json.put("customer", list);
			json.put("code", 1);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取合同信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toContractList.do")
	@ResponseBody
	public Map<String, Object> toContractList(HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("employeeId", user.getEmployeeId());
			map.put("contractStatus", 4);
			List<Contract> list = contractService.getContractList(map);
			json.put("list", list);
			json.put("code", 1);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 获取产品信息详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/productDetail.do")
	@ResponseBody
	public Map<String, Object> productDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Product product = productService.getProductById(Integer.parseInt(id));
			json.put("code", 0);
			json.put("product", product);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取产品信息列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appProductList.do")
	@ResponseBody
	public Map<String, Object> productList(PageInfo<Product> page, String productName, String isCustomer,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("productName", productName);
			if (isCustomer.equals("false")) {
				Customer sessionCustomer = (Customer) session.getAttribute("loginCustomer");
				map.put("riskRating", sessionCustomer.getRiskRating());
				map.put("overt", 1);
			}
			PageInfo<Product> pageInfo = productService.getPageInfoList(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取用户所属的客户信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appCustomerList.do")
	@ResponseBody
	public Map<String, Object> customerList(PageInfo<Customer> page, String customerName, String personLiable,
			HttpServletRequest request, HttpSession session, HttpServletResponse response, String startDate,
			String endDate) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// 获取登录用户信息
			Employee user = (Employee) session.getAttribute("loginUser");
			Map<String, Object> map = new HashMap<String, Object>();
			if (customerName != null && customerName != "") {// 添加用户输入查询条件
				map.put("customerName", customerName);
			}
			if (personLiable == null || personLiable == "") {
				personLiable = user.getEmployeeId();
			}
			if (startDate != null && startDate != "") {// 添加用户输入查询条件
				map.put("startDate", startDate);
			}
			if (endDate != null && endDate != "") {// 添加用户输入查询条件
				map.put("endDate", endDate + " 23:59:59");
			}
			map.put("personLiable", personLiable); // 查询属于自己的信息
			PageInfo<Customer> pageInfo = customerService.getCustomerPageInfoList(map, page);
			json.put("code", 0);
			json.put("pageInfo", pageInfo);
		} catch (Exception e) {
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取客户详情
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/customerDetail.do")
	@ResponseBody
	public Map<String, Object> customerDetail(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Customer customer = customerService.getCustomerById(id);
			json.put("code", 0);
			json.put("customer", customer);
			session.setAttribute("customerId", customer.getCustomerId());
		} catch (Exception e) {
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取登录客户信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getLoginCustomer.do")
	@ResponseBody
	public Map<String, Object> getLoginCustomer(HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Customer customer = (Customer) session.getAttribute("loginCustomer");
			json.put("code", 0);
			json.put("customer", customer);
		} catch (Exception e) {
			json.put("code", 1);
		}
		return json;
	}

	/**
	 * 获取手机验证码
	 * 
	 * @param type
	 *            1、改密，2、登录，3、换绑
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getPhoneCode.do")
	@ResponseBody
	public Map<String, Object> getPhoneCode(String type, String phone, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		String strCode = Integer.toString((int) ((Math.random() * 9 + 1) * 100000));
		if (SMSUtil.sendSMSBackPwd(phone, strCode)) {
			session.setAttribute("phoneCode", strCode);
			json.put("code", 0);
		} else {
			json.put("code", -1);
		}
		return json;
	}

	/**
	 * 更改密码
	 * 
	 * @param type
	 *            1、改密，2、登录，3、换绑
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getPhoneCodeUpdatePwd.do")
	@ResponseBody
	public Map<String, Object> getPhoneCodeUpdatePwd(String password, String pwdCode, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Customer custmer = (Customer) session.getAttribute("loginCustomer");
			String strCode = (String) session.getAttribute("phoneCode");
			if (strCode.equals(pwdCode)) {
				Customer customer = new Customer();
				customer.setCustomerId(custmer.getCustomerId());
				customer.setPassword(MD5Util.encrypt(password));
				customerService.saveCustomer(customer, "修改");
				json.put("code", 0);
			} else {
				json.put("code", 1);
			}
		} catch (Exception e) {
			json.put("code", -1);
			e.printStackTrace();
		}
		return json;
	}

	/**
	 * 客户获取自己的合同登记信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/customerContractList.do")
	@ResponseBody
	public Map<String, Object> contractList(String id, Integer limit, Integer page, String contractNum, String isCheck,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		PageInfo<Contract> pageInfo = new PageInfo<Contract>();
		try {
			if (Strings.isNotEmpty(contractNum)) {// 添加用户输入条件查询
				param.put("contractNum", contractNum);
			}
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			param.put("customerId", id);// 所属客户条件
			param.put("contractStatus", "4");// 所属客户条件
			PageInfo<Contract> pageRole = contractService.getPageInfoList(param, pageInfo);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("flag", 1);

		} catch (Exception e) {
			json.put("flag", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 获取预约资格
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/destineQualification.do")
	@ResponseBody
	public Map<String, Object> destineQualification(Integer id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			Product product = productService.getProductById(id);
			Double contractSum = contractService.getContractSum(product.getProductName());
			if (product.getLimitMoney()!=null) {
				if (product.getLimitMoney() > contractSum) {
					json.put("code", 0);
				} else {
					json.put("code", 1);
				}
			}else{
				json.put("code", 1);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
			e.printStackTrace();
		}
		return json;
	}
}
